<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
     <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true" ref="tableRef" >
      <el-table-column label="编号" align="center" prop="id" v-if="false"/>
      <el-table-column label="订单编号" align="center" prop="purchaseOrderId" />
      <el-table-column label="一级库整合单编号" align="center" prop="integrateId" v-if="false" />
      <el-table-column label="是否主单" align="center" prop="isMainOrder">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.IS_MAIN_ORDER" :value="scope.row.isMainOrder" />
        </template>
      </el-table-column>
      <el-table-column label="主单编号" align="center" prop="mainOrderId" />
      <el-table-column
        label="订单时间"
        align="center"
        prop="orderTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="供应商编号" align="center" prop="supplierId" v-if="false"/>
      <el-table-column label="合计价格" align="center" prop="totalPrice" />
      <el-table-column label="备注" align="center" prop="remark" v-if="false"/>
      <el-table-column label="状态" align="center" prop="status" v-if="false">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.ERP_SUPPLIER_ORDER_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="流程实例的编号" align="center" prop="processInstanceId" v-if="false"/>
      <el-table-column label="创建者" align="center" prop="creator" v-if="false"/>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
        v-if="false"
      />
    </el-table>
  </ContentWrap>
    <template #footer>
      <!-- <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button> -->
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import { InvoiceIntegrationApi, InvoiceIntegrationVO } from '@/api/erp/invoiceintegration'
import { PurPurchaseOrderApi, PurPurchaseOrderVO } from '@/api/erp/purpurchaseorder'

/** 发票整合 表单 */
defineOptions({ name: 'InvoiceIntegrationForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const loading = ref(true) // 列表的加载中
const list = ref<PurPurchaseOrderVO[]>([]) // 列表的数据
const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  integrationId: undefined,
  orderTotalPric: undefined,
  orderNum: undefined,
  status: undefined,
})

const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: String) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = "订单详情"
  resetForm()
  // 修改时，设置数据
  if (id) {
    loading.value = true
    try {
      list.value = await PurPurchaseOrderApi.getPurPurchaseOrderByOrderIntegrateId(id)
      loading.value = false

    } finally {
      loading.value = false

    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as InvoiceIntegrationVO
    if (formType.value === 'create') {
      await InvoiceIntegrationApi.createInvoiceIntegration(data)
      message.success(t('common.createSuccess'))
    } else {
      await InvoiceIntegrationApi.updateInvoiceIntegration(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    integrationId: undefined,
    orderTotalPric: undefined,
    orderNum: undefined,
    status: undefined,
  }
  formRef.value?.resetFields()
}
</script>